// wode/form/form.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",
    address:"", 
  },

  //选择地理位置
  chooseLocation:function(){
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          name: res.name,
          address: res.address,
        })
      },
    })
  },

  //选择开始日期
  bindDateChange: function (e) {
    var that = this;
    that.setData({
      date1: e.detail.value
    })
    console.log(that.data.date1);
  },
  //选择结束日期
  bindDateChangeTwo: function (e) {
    var that = this;
    that.setData({
      date2: e.detail.value
    })
    console.log(that.data.date2);
  },

//表单提交
submitData: function(e){
   var promoter = e.detail.value.promoter;
   var name = e.detail.value.name;
   var local = e.detail.value.local;
   var value = e.detail.value.value;
   var date1 = this.data.date1;
   var date2 = this.data.date2;
   console.log( this.data.date1)
    //发起post请求数据存入aspiration表中
    wx.request({
      url : "https://www.prebeauty.work:3001/api/aspiration",
      method: "POST",
      data: {
        holder: promoter,
        aspname: name,
        content: value,
        asplocal: local,
        start: date1,
        endtime: date2
      },
      header: {
        "content-type": "application/json"
      },
      success: function (res) {
        console.log(res.data); 
        wx.showToast({
          title: '发布志愿活动成功！',
          icon: 'success',
          duration: 2000
        })
        wx.redirectTo({
          url: '/wode/zhiyuan/zhiyuan', //跳转到志愿服务页面
        })
    },
  })

  //发起post请求数据存入heartfoot表中
  var userid = wx.getStorageSync('userid');
  var util = require('../../pages/utils/util.js');
  var time = util.formatTime(new Date())
  wx.request({
    url : "https://www.prebeauty.work:3001/api/heart",
    method: "POST",
    data: {
      userid: userid,
      type: 2, //发布类型
      time: time,
      activityname: name
    },
    header: {
      "content-type": "application/json"
    },
    success: function (res) {
      console.log(res.data); 
    },
  })

},


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      date1: "2020-01-01",
      date2: "2025-12-31"
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})